;; Rule object
;;
;; Copyright 2011:
;; James McDermott <jamesmichaelmcdermott@gmail.com>
;; Mark Wagy <mark.wagy@gmail.com>
;;
;; This file is part of p-gen, a p-systems and evolutionary system
;; written in Clojure.
;;
;; p-gen is licensed under GNU GPL v3.
;;
;; http://code.google.com/p/p-gen/

(ns psystem.rule (:use psystem.object) (:use psystem.util) (:use psystem.multiset))

;; rule definition
;; both lhs and rhs are collection of 'object's
;;(defrecord rule [lhs rhs])

(defn make-rule [lhs rhs]
  {:lhs lhs :rhs rhs})

(defn prompt-for-rule []
  (make-rule
    (prompt-read "left hand side")
    (prompt-read "right hand side")))

(defn save-rules [rules filename]
  (save-to-file rules filename))

(defn load-rules [filename]
  (load-from-file filename))

(defn print-rules [rules]
  (print-recs rules))

;; get values of rule's lhs objects
;;(defn lhs-values [rule]
;;  (psystem.object/get-values (:lhs rule)))
(defn lhs-values [rule]
  (:lhs rule))
    
;; get values of rule's rhs objects
;;(defn rhs-values [rule]
;;  (psystem.object/get-values (:rhs rule)))
(defn rhs-values [rule]
  (:rhs rule))

;; apply a rule to a set of objects
;; TODO: test - this is a rough draft
(defn apply-rule [rule objects]
  (let [[beg end] (psystem.multiset/subvec-position 
                    objects
                    (lhs-values rule))]
    (println "beg: " beg)
    (println "end: " end)
    (concat 
      (subvec (rhs-values rule) 0 beg) 
      (subvec (rhs-values rule) beg end) 
      (subvec (rhs-values rule) end (count rule)))))




  